# ==================================================================================
# 游戏资源构建系统 - 主Makefile
# ==================================================================================
# 功能说明：
#   本文件是整个资源构建系统的主入口，负责编排所有构建步骤
#   按照正确的依赖顺序执行各个子makefile
# 
# 构建流程：
#   1. 生成通用协议（Common Protocol）
#   2. 生成客户端-服务器协议（CS Protocol）
#   3. 生成Excel处理makefile
#   4. 【patch版本】复制旧的MaxNum定义文件（用于保持数组大小不变）
#   5. 处理Excel文件，生成资源配置
#   6. 生成资源元数据
#   7. 生成DescStore描述文件
#   8. 批量处理资源元数据的makefile
#   9. 生成服务器间协议（SS Protocol）
#  10. 生成数据库协议（DB Protocol）
#  11. 生成存储协议（Store Protocol）
# 
# 使用方法：
#   make                                      # GAME_DIR为空，跳过构建
#   make GAME_DIR=LieRen                      # 指定游戏项目进行大版本构建
#   make GAME_DIR=Other                       # 指定其他游戏项目
#   make GAME_DIR=LieRen GAME_VERSION_PATCH=1  # 组合使用：指定项目 + patch版本
#   make clean                                # 清理构建临时文件
# ==================================================================================
SHELL := /bin/bash
include ./define.makefile
.PHONY:all clean

# 记录构建开始时间（用于计算总耗时）
START_TIME = $(shell cat /proc/uptime | awk -F "." '{print $$1}')

# ==================================================================================
# 主构建目标
# ==================================================================================
all:
ifeq ($(GAME_DIR),)
	@echo "==================================================================================="
	@echo "GAME_DIR为空，跳过game目录资源构建"
	@echo "如需构建游戏资源，请指定GAME_DIR参数，例如: make GAME_DIR=LieRen"
	@echo "==================================================================================="
else
	@echo "==================================================================================="
	@echo "开始资源构建..."
	@echo "游戏项目: GAME_DIR = ${GAME_DIR}"
	@echo "协议路径: ${PROTOCOL_PATH}"
	@echo "版本类型: GAME_VERSION_PATCH = ${GAME_VERSION_PATCH} (0=大版本, 1=patch版本)"
	@echo "==================================================================================="
	@rm default.log -rf
	
	# ----------------------------------
	# 步骤1: 生成通用逻辑协议
	# ----------------------------------
	@echo "\n[1/11] 生成通用逻辑协议（Common Protocol）..."
	make -f ${MAKE_PROTOC_COMMON_GEN} 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤2: 生成客户端-服务器协议
	# ----------------------------------
	@echo "\n[2/11] 生成客户端-服务器协议（CS Protocol）..."
	make -f ${MAKE_PROTOC_CS_GEN} 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤3: 生成Excel处理makefile
	# ----------------------------------
	@echo "\n[3/11] 生成Excel处理makefile..."
	make -f ${MAKE_EXCELMAKEFILE_GEN}  2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤4: patch版本特殊处理
	# ----------------------------------
ifeq (${GAME_VERSION_PATCH},1)
	@echo "\n[4/11] patch版本：复制旧版本的MaxNum定义文件..."
	@echo "  作用：保持数组大小不变，确保热更新兼容性"
	${FILE_COPY_EXE} --work="filecopy" --src="${DESC_STORE_PATH}/NFDescStoreMaxNumDefine.h" --dst=${PROTOCGEN_FILE_PATH}/
else
	@echo "\n[4/11] 大版本：跳过MaxNum文件复制"
endif
	
	# ----------------------------------
	# 步骤5: 处理Excel文件
	# ----------------------------------
	@echo "\n[5/11] 处理Excel文件，生成资源配置..."
	make -f ${MAKE_EXCEL_GEN}  2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤6: 生成资源元数据
	# ----------------------------------
	@echo "\n[6/11] 生成资源元数据（ResMetas）..."
	make -f ${MAKE_RESMETAS_GEN}  2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤7: 生成DescStore描述文件
	# ----------------------------------
	@echo "\n[7/11] 生成DescStore描述文件..."
	make -f ${MAKE_DESCSTORE_GEN}  2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤8: 批量处理资源元数据makefile
	# ----------------------------------
	@echo "\n[8/11] 批量处理资源元数据makefile..."
	mk_list=`ls ${RESDB_META_PATH}/*.makefile`;\
	for mk_file in $${mk_list};do\
		echo "  处理: $$mk_file";\
		make -f $$mk_file 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;\
	done
	
	# ----------------------------------
	# 步骤9: 生成服务器间协议
	# ----------------------------------
	@echo "\n[9/11] 生成服务器间协议（SS Protocol）..."
	make -f ${MAKE_PROTOC_SVR_GEN} 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤10: 生成数据库协议
	# ----------------------------------
	@echo "\n[10/11] 生成数据库协议（DB Protocol）..."
	make -f ${MAKE_PROTOC_DB_GEN} 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 步骤11: 生成存储协议
	# ----------------------------------
	@echo "\n[11/11] 生成存储协议（Store Protocol）..."
	make -f ${MAKE_PROTOC_STORE_GEN} 2>&1 | tee -a ${MAKE_COMPILE_LOG} 2>&1; if [ $${PIPESTATUS[0]} -ne 0 ]; then exit 1;fi;
	
	# ----------------------------------
	# 构建完成，统计耗时
	# ----------------------------------
	@current_time=`cat /proc/uptime | awk -F "." '{print $$1}'`; \
	time_interval=`expr $${current_time} - $(START_TIME)`; \
	runtime=`date -u -d @$${time_interval} +%Hh:%Mm:%Ss`; \
	echo "######## runtime: $${runtime} ########"
endif

# ==================================================================================
# 清理目标
# ==================================================================================
clean:
	@echo "清理构建临时文件..."
	rm ${GAME_BUILD_PATH}/* -rf
	@echo "清理完成！"




